<?php

class Ain_ua extends Grabber_Abstract
{
	
	public function _init()
	{
	}
	
	public function parsingList(  )
	{
		$this->_parsingList();
	}
	
	protected function _parsingList()
	{
		$parser_data = $this->_oParser->getParserForUpdate();
		$html = $this->_oBrowser->get( $parser_data->url );

		$matched = $this->_match('post_title">.*<a href="(.*)".*>(.*)</a', $html);

		if( !empty( $matched[1] ) && !empty( $matched[2] ) )
		{
			foreach( $matched[1] as $key => $value )
			{
				if( !$this->_oNews->check( $matched[2][$key] ) )
				{
					$this->_oNews->insert( $parser_data->id, 1, $this->_stripTags( strip_tags( $matched[2][$key] ) ), $matched[1][$key], $this->_sql_date() );
				}
				
				echo $matched[1][$key] . '<br />';
			}
		}
		
		if( $parser_data->id )
		{
			$this->_oParser->update( $parser_data->id, false, false, false, $this->_sql_date(self::NEXT_PARSING_LIST) );
		}
	}
	
	public function parsingNews()
	{
		$this->_parsingNews();
	}
	
	protected function _parsingNews()
	{
		$_used_users = array();
		
		$news_for_parsing = $this->_oNews->getNewsForParsing( false, 2 );
		
		foreach( $news_for_parsing as $key => $news_row )
		{
			$parser_row = $this->_oParser->get( array('id = ' => $news->parser_id) );
			
			$html = $this->_oBrowser->get( $news_row->url );
			$news = $this->_getNews( $html );
			
			$news_id = $this->_insertNews( $parser_row, $news_row, $news );

			$this->_oNews->update( $news_row->id, false, $news_id, false, false, $this->_sql_date( -1 ), false);
		}
		echo "hello debug end"; exit;
	}
	
	protected function _insertNews( $parser_row, $news_row, $news )
	{
		if( empty( $news['news'] ) )
			return false;

		$user_id = $this->_oUsers->getRandomUser( $_used_users );
		$_used_users[] = $user_id;

		$link_id = $this->_oLinks->insert( $user_id, $parser_row->group_id, $news_row->title, $news['news'], $news_row->url, '', '' );
		
		$this->_oUsers->updateKarma( $user_id, 'review' );
		$this->_oUsers->updateKarma( $user_id, 'vote' );
		
		if( !empty( $news['comments'] ) )
		{
			foreach( $news['comments'] as $comment )
			{
				$user_id = $this->_oUsers->getRandomUser( $_used_users );
				$_used_users[] = $user_id;
				
				$this->_oComments->insert( $user_id, $link_id, $comment);
				$this->_oUsers->updateKarma( $user_id, 'comment' );
				$this->_oUsers->updateKarma( $user_id, 'vote' );
			}
		}
		
		return $link_id;
	}


	/**
	 * Парсит html, витягивает полную статью
	 *
	 * @param type $html - string
	 * @return array('news'=>string, 'comments'=>array());
	 */
	protected function _getNews( $html )
	{
		$result = array( 'news' => '', 'comments' => array() );
		
		$html = $this->_cutSpaces( $html );
		
		$pattern = '<div class="entry-content">(.*)</div>';
		$matched = $this->_match( $pattern, $html );
		
		if( !empty( $matched[1] ) && !empty( $matched[1][0] ) )
			$result['news'] = $matched[1][0];
		
		$pattern = 'class="dsq-comment-message".*><p>(.*)</p>.*</div>';
		$matched = $this->_match( $pattern, $html );
		
		if( !empty( $matched[1] ) )
			$result['comments'] = $matched[1];
		
		return $result;
	}
	
}




/*

$oUsers   = new Users( $db );
$oLink    = new Links( $db );
$oComments= new Comments( $db );
$oProxy   = new Proxy();
$oBrowser = new Browser();
$oFile    = new File( 'tophotels.ru' );
echo 'good'; exit;
$data = $oFile->get( 'hotels' );
if( !$data )
{
    $url = 'http://www.tophotels.ru/main/top100/100/';
    $result = $oBrowser->get( $url );
    $result = iconv('windows-1251', 'utf-8', $result);
    $result = preg_replace('/\n/iU', '', $result);

    $matched = array();
    //http://www.tophotels.ru/main/hotel/al5055/
    preg_match_all('!class="parity\d+".*title="(.*)"\ target="_blank"\ href="(.*)"!iU', $result, $matched);

    //print_r($matched);

    $data = array();
    foreach( $matched[1] as $key => $title )
    {
	$data[$matched[2][$key]] = $title;
	echo '<a href="'. $matched[2][$key] .'">'. $title .'</a><br />';
    }
    
    $oFile->save( 'hotels', $data );
}

$data = (array)$data;

$html = '';
$title_link = '';
foreach( $data as $url => $title )
{
    $html = $oBrowser->get( $url );
    $title_link = $title;
    if( $html )
    {
	unset( $data[$url] );
	break;
    }
}

$html = iconv('windows-1251', 'utf-8', $html);

$_used_users = array();
$matched = array();
$link_id = 0;

preg_match_all('!class="review-text-premium">(.*)<!iU', $html, $matched);
if( !empty( $matched[1] ) && !empty( $matched[1][0] ) )
{
    $_user_id = $oUsers->getRandomUser( $_used_users );
    $_used_users[] = $_user_id;
    
//    $link_id = $oLink->addLink($_user_id, 1, $title_link, $matched[1][0]);
    $link_id = 1;
}
//echo $html;
$matched = array();
preg_match_all('!class="review-text">(\n?.+)\n?<d!iU', $html, $matched);

print_r( $matched ); exit();

if( !empty( $matched[1] ) && !empty( $matched[1][0] ) )
{
    $_user_id = $oUsers->getRandomUser( $_used_users );
    $_used_users[] = $_user_id;
    
    $link_id = $oComments->addComment($_user_id, $link_id, $title_link, $matched[1][0]);
}

print_r( $matched[1][0] );





//$_used_users = array();
//
//for($i=0; $i < 4; $i++)
//{
//    $_user_id = $oUsers->getRandomUser($_used_users);
//    $_used_users[] = $_user_id;
//}
//
//$oLink->addLink($_user_id, 1, 'Бім бім', 'hello content');
//$oUsers->updateKarma($_user_id, 'review');
//echo 'user_id: '. $_user_id . ';';
//
//print_r($_used_users); exit();
 
  
 */